package com.bdq.basic.domain.service.impl;

import cn.hutool.core.convert.Convert;
import com.bdq.basic.domain.model.dto.CategoryGetDomainDTO;
import com.bdq.basic.domain.model.dto.RegionDetailGetDomainDTO;
import com.bdq.basic.domain.model.dto.RegionGetDomainDTO;
import com.bdq.basic.domain.model.dto.req.CategoryGetReqDTO;
import com.bdq.basic.domain.model.dto.req.RegionGetReqDTO;
import com.bdq.basic.domain.service.BaseDomainService;
import com.bdq.basic.infrastructure.persistence.mysql.pojo.BaseCategory;
import com.bdq.basic.infrastructure.persistence.mysql.pojo.BaseRegion;
import com.bdq.basic.infrastructure.persistence.mysql.service.BaseCategoryService;
import com.bdq.basic.infrastructure.persistence.mysql.service.BaseRegionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * @author malei
 * @date 2024-05-10 11:41
 */
@Service
public class BaseDomainServiceImpl implements BaseDomainService {

    @Resource
    private BaseCategoryService baseCategoryService;

    @Resource
    private BaseRegionService baseRegionService;

    @Override
    public List<CategoryGetDomainDTO> getCategory(CategoryGetReqDTO dto) {
        // 1 查询
        List<BaseCategory> categoryList = baseCategoryService.getByChannel(dto.getChannel());
        // 2 排序
        categoryList.sort(Comparator.comparingInt(BaseCategory::getSort));
        return Convert.toList(CategoryGetDomainDTO.class, categoryList);
    }

    @Override
    public RegionGetDomainDTO getRegion(RegionGetReqDTO dto) {
        // 1 查询
        List<BaseRegion> regionList = baseRegionService.listRegion(dto.getParentRegionId());
        // 2 排序
        regionList.sort(Comparator.comparing(BaseRegion::getId));
        // 3 返回
        RegionGetDomainDTO domainDTO = new RegionGetDomainDTO();
        domainDTO.setRegionType(regionList.get(0).getRegionType());
        List<RegionDetailGetDomainDTO> detailList = Convert.toList(RegionDetailGetDomainDTO.class, regionList);
        domainDTO.setRegionList(detailList);
        return domainDTO;
    }
}
